#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    struct ListNode* slow, * fast;
    slow = fast = head;
    if (head->next == NULL)
        return NULL;
    while (n--)
    {
        fast = fast->next;
    }
    if (fast == NULL)
        return slow->next;
    while (fast->next)
    {
        fast = fast->next;
        slow = slow->next;
    }
    struct ListNode* tmp = slow->next;
    slow->next = slow->next->next;
    free(tmp);

    return head;
}